//
//  BCLabguageSettingManager.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/14.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCLabguageSettingManager.h"


static NSString *BCLocalLanguageTypeKey = @"LocalLanguageKey";
/**
 语言
 */
static NSString *BCLanguageTypeEnglish_en = @"en";//英文 英式英文、美式英文...
static NSString *BCLanguageTypeChineseSimplified_zhHans = @"zh-Hans";//简体中文
static NSString *BCLanguageTypeChineseTraditional_zhHant = @"zh-Hant";//繁体中文
static NSString *BCLanguageTypeJapanese_ja = @"ja";//日文

@interface BCLabguageSettingManager ()

/**
 语言包
 */
@property (nonatomic, strong) NSBundle *languageBundle;

@end

@implementation BCLabguageSettingManager

+ (instancetype)shared {
    static BCLabguageSettingManager *sm = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sm = [[BCLabguageSettingManager alloc] init];
    });
    return sm;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initLanguage];
    }
    return self;
}

#pragma mark - 应用启动时初始化语言
/**
 本地语言id
 简体中文 -> zh-Hans
 繁体中文 -> zh-Hant
 中文香港 -> zh-HK
 */
- (void)initLanguage {
    NSString *preferredLanguage;
    id localLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:BCLocalLanguageTypeKey];
    if (localLanguage&&[localLanguage isKindOfClass:NSString.class]) {
        preferredLanguage = localLanguage;
    }
    if (!preferredLanguage) {
        preferredLanguage = [NSLocale preferredLanguages].firstObject;//当前
    }
    NSLog(@"%@",[NSLocale preferredLanguages].firstObject);
    
    /**
     1.en开头的全部处理成英文
     2.zh开头的是中文要分开处理
     */
    if ([preferredLanguage hasPrefix:@"en"]) {
        self.appLanguage = BCLanguageTypeEnglish;
    }else if ([preferredLanguage hasPrefix:@"zh"]) {
        if ([preferredLanguage rangeOfString:BCLanguageTypeChineseSimplified_zhHans].location!=NSNotFound) {//识别简体中文
            self.appLanguage = BCLanguageTypeChineseSimplified;
        }else if ([preferredLanguage rangeOfString:BCLanguageTypeChineseTraditional_zhHant].location!=NSNotFound) {//繁体
            self.appLanguage = BCLanguageTypeChineseTraditional;
        }else if ([preferredLanguage rangeOfString:@"zh-HK"].location!=NSNotFound) {//香港返回繁体
            self.appLanguage = BCLanguageTypeChineseTraditional;
        }
    }else if ([preferredLanguage hasPrefix:@"ja"]) {
        self.appLanguage = BCLanguageTypeJapanese;
    }
    else {
        self.appLanguage = BCLanguageTypeEnglish;
    }
}

#pragma mark - 设置语言
- (void)setAppLanguage:(BCLanguageType)appLanguage {
    _appLanguage = appLanguage;
    //
    NSString *resourceName = BCLanguageTypeEnglish_en;
    switch (appLanguage) {
        case BCLanguageTypeEnglish:
        {
            resourceName = BCLanguageTypeEnglish_en;
        }
            break;
        case BCLanguageTypeChineseSimplified: {
            resourceName = BCLanguageTypeChineseSimplified_zhHans;
        }
            break;
        case BCLanguageTypeChineseTraditional: {
            resourceName = BCLanguageTypeChineseTraditional_zhHant;
        }
            break;
        case BCLanguageTypeJapanese:
        {
//            resourceName = BCLanguageTypeJapanese_ja;
        }
            break;
    }
    //固化在UserDefault中
    [[NSUserDefaults standardUserDefaults] setObject:resourceName forKey:BCLocalLanguageTypeKey];
    //
    NSString *languageBundlePath = [[NSBundle mainBundle] pathForResource:resourceName ofType:@"lproj"];
    self.languageBundle = [NSBundle bundleWithPath:languageBundlePath];
}


/**
 {
 "title": "简体中文",
 "sign": "zh-CN",
 "has_default": 1,
 "version": ""
 },
 {
 "title": "繁體中文",
 "sign": "zh-TW",
 "has_default": 0,
 "version": ""
 }
 */
- (NSString *)requestLang {
    switch (self.appLanguage) {
        case BCLanguageTypeEnglish:
            return @"en";
            break;
        case BCLanguageTypeChineseSimplified:
            return @"zh-CN";
            break;
        case BCLanguageTypeChineseTraditional:
            return @"zh-TW";
            break;
        default:
            return nil;
            break;
    }
    return nil;
}

- (NSString *)currentLanguageString {
    return [self languageStringFromType:self.appLanguage];
}
//
/**
 根据语言获取当前对应string
 */
- (NSString *)languageStringFromType:(BCLanguageType)type {
    switch (type) {
        case BCLanguageTypeEnglish:
            return BCLocalizedKey(@"English");
            break;
        case BCLanguageTypeChineseSimplified:
            return BCLocalizedKey(@"Chinese Simplified");
            break;
        case BCLanguageTypeChineseTraditional:
            return BCLocalizedKey(@"Chinese Traditional");
            break;
        default:
            return nil;
            break;
    }
}





+ (NSString *)localizedStringForKey:(NSString *)key value:(id)value table:(NSString *)table {
    return [[BCLabguageSettingManager shared].languageBundle localizedStringForKey:key value:value table:table];
}
+ (NSString *)localizedStringFromLanguageTableForKey:(NSString *)key {
    return [self localizedStringForKey:key value:nil table:@"BCLanguage"];
}


/**
 登录模块多语言语言
 */
+ (NSString *)localizedStringFromLoginLocalizationTableForKey:(NSString *)key {
    return [self localizedStringForKey:key value:nil table:@"GSCommonLoginLocalization"];
}

@end
